package com.xsq;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;

import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.util.ConvertUtils;

import com.common.utils.AppManager;
import com.common.utils.LogUtils;
import com.xsq.util.HttpInterface;
import com.xsq.util.ServerUtils;
import com.xsq.util.WeiboDialogUtils;

@SuppressLint({ "HandlerLeak", "SimpleDateFormat" })
public class BaseActivity extends Activity {


	public String packageName = "com.xsq";
	public HttpInterface hi = null;
	public ServerUtils serverUtils;
	
	
	public WebView myWebView;
	public SharedPreferences preferences;
	public SharedPreferences.Editor editor;
	
	
	public Dialog mWeiboDialog;
	public AlertDialog  alertDialog1; // 提示对话框
	public AlertDialog  alertDialog2; //进度条对话框
	
	
	private static final int SHOW_WAITING = 50001;
	private static final int HIDE_WAITING = 50002;
	
	private static final int SHOW_DAY_PICKER = 80001;
	private static final int SET_CONTROL_VALUE = 80002;
	
	private static final int POS_PAGE_COM_MSG = 80003;
	
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    AppManager.getAppManager().addActivity(this);  
	}
	
	public void onDestroyed(){
		super.onDestroy();
		AppManager.getAppManager().finishActivity(this);  
	}
	
	public Handler baseHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case SHOW_WAITING:
				String content = msg.getData().getString("msg");
				showWaiting(content);
				break;
			case HIDE_WAITING:
				hide_loading();
				break;
				
			case SHOW_DAY_PICKER:
				showYearMonthDay(msg.getData().getString("id"), msg.getData().getString("ctype"));
				break;
			case SET_CONTROL_VALUE:
				setControlValue(msg.getData().getString("id"), msg.getData().getString("value"));
				break;
				
			case POS_PAGE_COM_MSG:
				page_showMsg(msg.getData().getString("showMsg"));
				break;	

			}
		}
	};
	
	public void showWaiting(String msg){
		Message message = new Message();
		message.what = SHOW_WAITING;
		Bundle bundle  = new Bundle();
		bundle.putString("msg", msg);
		message.setData(bundle);
		baseHandler.sendMessage(message);
	}

	
	public void hideWaiting(){
		baseHandler.sendEmptyMessage(HIDE_WAITING);
	}
	
	public void page_showMsg(String msg){
		String call = "javascript:comShowMsg('"+msg+"');";
		
		myWebView.loadUrl(call);
	}
	

	public void showDatePicker(String id,String ctype){
		Message message = new Message();
		message.what = SHOW_DAY_PICKER;
		Bundle bundle = new Bundle();
		bundle.putString("id", id);
		bundle.putString("ctype", ctype);
		message.setData(bundle);
		
		baseHandler.sendMessage(message);
	}

	
	/**
	 * 通过显示信息弹出层方法
	 */
	public void pageShowMsg(String msg,Long delayMillis){
		
		Message message = new Message();
		message.what = POS_PAGE_COM_MSG;
		Bundle bundle = new Bundle();
		bundle.putString("showMsg", msg);
		message.setData(bundle);
		if(delayMillis==null){
			baseHandler.sendMessage(message);
		}else{
			baseHandler.sendMessageDelayed(message,delayMillis);
		}
		
	}

	
	
	/*
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getAppManager().addActivity(this);
	}
	protected void onDestroy() {
		super.onDestroy();
		AppManager.getAppManager().finishActivity(this);
	}
	*/
	
	// 透明导航栏，就是下面三个虚拟按钮
	public void initStyle(){
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
		getWindow().setAttributes(params);
		View decorView = getWindow().getDecorView();
		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
		decorView.setSystemUiVisibility(uiOptions);
	}
	
	
	public String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder();
		if (src == null || src.length <= 0) {
			return null;
		}
		char[] buffer = new char[2];
		for (int i = 0; i < src.length; i++) {
			buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
			buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
			LogUtils.info(buffer);
			stringBuilder.append(buffer);
		}
		return stringBuilder.toString().toUpperCase();
	}
	
	
	public void show_loading(String msg) {
		mWeiboDialog = WeiboDialogUtils.createLoadingDialog(this, msg);
	}

	public void hide_loading() {
		if (mWeiboDialog != null) {
			WeiboDialogUtils.closeDialog(mWeiboDialog);
		}
	}
	
	
	
	
	
	
	public void setControlValue(String id,String value){
		
		String call = "javascript:setControlValue('"+id+"','"+value+"');";
		
		myWebView.loadUrl(call);
	}
	
	public void showYearMonthDay(final String id,final String ctype){
		
		
		Calendar calendar = Calendar.getInstance();
		int C_YEAR = calendar.get(Calendar.YEAR);
		int C_MONTH = calendar.get(Calendar.MONTH);
		int C_DAY = calendar.get(Calendar.DAY_OF_MONTH);
		
		
		
		Calendar lastMonth = Calendar.getInstance();
		lastMonth.add(Calendar.MONTH, -2);//2个月前
		int FRONT_C_YEAR = lastMonth.get(Calendar.YEAR);
		int FRONT_C_MONTH = lastMonth.get(Calendar.MONTH);
		int FRONT_C_DAY = lastMonth.get(Calendar.DAY_OF_MONTH);
		
		
		
		
		if (ctype.equals("1")) {
			final DatePicker picker = new DatePicker(this);
			picker.setCanceledOnTouchOutside(true);
			picker.setUseWeight(true);
			picker.setTopPadding(ConvertUtils.toPx(this, 10));
			picker.setRangeEnd(C_YEAR, C_MONTH+1, C_DAY);
			picker.setRangeStart(FRONT_C_YEAR, FRONT_C_MONTH+1, FRONT_C_DAY);
			picker.setSelectedItem(C_YEAR, C_MONTH+1, C_DAY);

			picker.setResetWhileWheel(false);
			picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
				@Override
				public void onDatePicked(String year, String month, String day) {
					//Toast.makeText(getApplicationContext(), year + "-" + month + "-" + day, Toast.LENGTH_LONG).show();

					String value = year + "-" + month + "-" + day;
					Message message = new Message();
					Bundle bundle = new Bundle();
					bundle.putString("id", id);
					bundle.putString("value", value);
					message.setData(bundle);
					message.what = SET_CONTROL_VALUE;
					baseHandler.sendMessage(message);
					
				}
			});
			picker.setOnWheelListener(new DatePicker.OnWheelListener() {
				@Override
				public void onYearWheeled(int index, String year) {
					picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
				}

				@Override
				public void onMonthWheeled(int index, String month) {
					picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
				}

				@Override
				public void onDayWheeled(int index, String day) {
					picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
				}
			});
			picker.show();

		} else if (ctype.equals("2")) {

			DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
				public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
					int iyear = view.getYear();// 年
					String value = iyear + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
					

					Message message = new Message();
					Bundle bundle = new Bundle();
					bundle.putString("id", id);
					bundle.putString("value", value);
					message.setData(bundle);
					message.what = SET_CONTROL_VALUE;
					baseHandler.sendMessage(message);

				}
			}, C_YEAR, C_MONTH, C_DAY);// 初始年份,初始月份-1 ，初始日期
			android.widget.DatePicker dpp = dp.getDatePicker();
			
			dpp.setMaxDate(new Date().getTime());
			dpp.setMinDate(lastMonth.getTime().getTime());
			
			
			dp.show();
		}
		
	}
	
	
	public String getSysAdmin(){
		return preferences.getString(Constant.CACHE_PWD_SYSADMIN, Constant.sysAdmin);
	}
	
	public String getOperAdmin(){
		return preferences.getString(Constant.CACHE_PWD_USER, Constant.userAdmin);
	}
	
	public String getCardType(){
		return  preferences.getString(Constant.CACHE_CARDTYPE, "1"); 
	}
	
	public String getPosNo(){
		return preferences.getString(Constant.CACHE_POSNO, ""); 
	}
	
	public String getWinNo(){
		return preferences.getString(Constant.CACHE_WINNO, ""); 
	}
	
	public String getServerInfo(){
		return preferences.getString(Constant.CACHE_SERVERINFO, "");
	}
	
	public String getUpdateAlert(){
		return preferences.getString(Constant.CACHE_UPDATE_ALERT, "");
	}
	
	public String getWinName(){
		return preferences.getString(Constant.CACHE_WINNAME, "--");
	}
	
	public String getDeviceId(){
		return preferences.getString(Constant.CACHE_DEVICEID, "");
	}
	
	public String getPrintCodeVersion(){
		return preferences.getString(Constant.CACHE_PRINTER_CODE, "0");
	}
	
	public String getConsumePrintMode(){
		return preferences.getString(Constant.CACHE_CONSUME_PRINT_MODE, "0");
	}
	
	public String getConsumePwdMode(){
		return preferences.getString(Constant.CACHE_CONSUME_PWD_MODE, "0");
	}
	
	public String getSysTitle(){
		return preferences.getString(Constant.CACHE_SYS_TITLE, "");
	}
	
	public String getPrintTitle(){
		return preferences.getString(Constant.CACHE_PRINT_TITLE, "消费单据");
	}
	
	public String getXfxx(){
		return preferences.getString(Constant.CACHE_XFXX, "0");
	}
	
	/**
	 * 是否定额消费
	 * @return
	 */
	public String getSFQuatoConsume(){
		return preferences.getString(Constant.CACHE_CONSUME_QUATO, "0");
	}
	
	public String getQuatoConsumeMoney(){
		return preferences.getString(Constant.CACHE_CONSUME_QUATO_MONEY, "0");
	}
	
}
